package com.sanyang.pda.ui.manualdelivery;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sanyang.pda.adapter.ManualDeliveryAdapter;
import com.sanyang.pda.bean.ManualDeliveryBean;
import com.sanyang.pda.contract.ManualDeliveryContract;
import com.sanyang.pda.presenter.ManualDeliveryPresenter;
import com.sanyang.pda.utils.StringConfig;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AppActivityKey.MANUALDELIVERYACTIVITY)
public class ManualDeliveryActivity extends BaseMvpActivity<ManualDeliveryPresenter> implements ManualDeliveryContract.View, OnRefreshListener, OnLoadMoreListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.title)
    FrameLayout title;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.smart)
    SmartRefreshLayout smart;
    private int num=1;
    private ManualDeliveryAdapter manualDeliveryAdapter;

    @Override
    public int getView() {
        return R.layout.activity_manual_delivery;
    }

    @Override
    public void initView() {
        smart.setOnRefreshListener(this);
        smart.setOnLoadMoreListener(this);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        manualDeliveryAdapter = new ManualDeliveryAdapter(R.layout.item_manual_pallet, new ArrayList<>());
        rvList.setAdapter(manualDeliveryAdapter);
    }

    @Override
    public void initLinsenterner() {
        manualDeliveryAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                List<ManualDeliveryBean> list = (List<ManualDeliveryBean>) adapter.getData();
                Bundle bundle = new Bundle();
                bundle.putString(StringConfig.ENTRYNO, list.get(position).getEntryNo());
                startIntent(AppActivityKey.MANUALDELIVERYDETAILACTIVITY, bundle);
            }
        });
    }

    @Override
    public void getData() {
        smart.autoRefresh();
    }

    @Override
    public ManualDeliveryPresenter getPresenter() {
        return new ManualDeliveryPresenter();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finishActivity();
    }

    @Override
    public void setList(List<ManualDeliveryBean> list) {
        smart.finishLoadMore();
        smart.finishRefresh();
        if (num == 1){
            manualDeliveryAdapter.setNewInstance(list);
        }else {
            manualDeliveryAdapter.addData(list);
        }
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        num++;
        presenter.manualDeliveryList(num);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        num=1;
        presenter.manualDeliveryList(num);
    }
    @Override
    public void showMessage(String msg) {
        super.showMessage(msg);
        smart.finishLoadMore();
        smart.finishRefresh();
    }
}
